package com.dc.servlet;

import com.alibaba.fastjson.JSON;
import com.dc.bean.model.BaseModel;
import com.dc.bean.utils.BaseDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "AddUserServlet", urlPatterns = {"/AddUser"})
public class AddUserServlet extends HttpServlet {
    BaseDao baseDao = new BaseDao();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

req.setCharacterEncoding("utf-8");
    Object[] param = new Object[11];
    param[0] = req.getParameter("name");
    param[1] = req.getParameter("trueName");
    param[2] = req.getParameter("cardId");
    param[3] = req.getParameter("email");
    param[4] = req.getParameter("telephone");
    param[5] = req.getParameter("qqId");
    param[6] = req.getParameter("pwd");
    param[7] = new Date();
    param[8] = req.getParameter("question");
    param[9] = req.getParameter("answer");
    param[10] = req.getParameter("state");
    String sql ="INSERT into t_user (name,trueName,cardId,email,telephone,qqId,pwd,regTime,question,answer,state) values(?,?,?,?,?,?,?,?,?,?,?)";
    int result = baseDao.update(sql,param);
    BaseModel baseModel = new BaseModel();
        if(result>0){
        baseModel.setCode(1);
        baseModel.setMassage("新增用户成功");
    }else{
        baseModel.setCode(-1);
        baseModel.setMassage("新增用户失败");
    }
    String baseModelStr=JSON.toJSONString(baseModel);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(baseModelStr);
        resp.getWriter().flush();
        resp.getWriter().close();
}
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
